package org.txazo.stat.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Cookie工具类
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 * 
 */
public class CookieUtil {

	/**
	 * 清除Cookie
	 */
	public static void clearCookie(String name, HttpServletRequest request,
			HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		String key = null;
		for (Cookie cookie : cookies) {
			key = cookie.getName();
			if (StringUtil.isNotEmpty(key) && key.equals(name)) {
				cookie.setValue(null);
				cookie.setMaxAge(0);
				cookie.setPath("/");

				response.addCookie(cookie);
			}
		}
	}

	/**
	 * 添加Cookie
	 */
	public static void addCookie(String name, String value, String path,
			HttpServletResponse response) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(ConstantsUtil.COOKIE_MAXAGE);
		cookie.setSecure(false);
		cookie.setPath(path);

		response.addCookie(cookie);
	}

}
